package com.knowif.util.parseUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @author xingyao
 * @time 上午9:19:16
 */
@SuppressWarnings("all")
public class XmlUtils {
	
	// 得到指定xml文件的decument对象
	public static Document getDocument(String fileName) {
		SAXReader reader = new SAXReader();
		try {
//			String path = XmlUtils.class.getClassLoader().getResource(fileName).toString();
//			path = path.replace("file:/", "");
//			return reader.read(new File(path));
			return reader.read(XmlUtils.class.getClassLoader().getResourceAsStream(fileName));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		Document d = XmlUtils.getDocument("com/stusiri/xmlConfig/proxyConfig.xml");
		String s = XmlUtils.getNodeContextName(XmlUtils.getDocument("com/stusiri/xmlConfig/proxyConfig.xml"),
				"proxy","baseDaoProxy", "LogClassName");
		System.out.println(s);
	}

	/**
	 * 得到id的�?与rootId相同的某个节点的�?
	 * 
	 * @param document
	 *            ：某个xml的document对象
	 * @param rootName
	 *            ：根节点的名�?如Resource文件中应该是type
	 * @param rootId
	 *            ：id名称:如相应type的�?
	 * @param nodeName
	 *            ：节点名称：如Resource文件中应该是LogClassName，然后就能得到相应的�?
	 * @return
	 */
	public static String getNodeContextName(Document document, String rootName,
			String rootId, String nodeName) {
		Element root = document.getRootElement();
		List<Element> list = root.elements(rootName);
		for (Element element : list) {
			if (rootId.equals(element.attribute("id").getValue())) {
				return element.elementText(nodeName);
			}
		}
		return null;
	}

	// 将内存中的document写入xml文件�?
	private static void write2Xml(String fileName, Document document)
			throws IOException {
		URL url = XmlUtils.class.getClassLoader().getResource(fileName);
		String realpath = url.getPath();
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter writer = new XMLWriter(new FileOutputStream(realpath), format);
		writer.write(document);
		writer.close();
	}

	public static void getNodes(Element node, Map<String,String> map) {
		map.put(node.getName(), node.getTextTrim());
		List<Attribute> listAttr=node.attributes();
		for(Attribute attr:listAttr){
			String name=attr.getName();
			String value=attr.getValue();
			map.put(name, value);
		}
		List<Element> listElement=node.elements();
		for(Element e:listElement){
			getNodes(e, map);
		}
	}

	public static Map<String,String> xmlToMap(String xml){
		Map<String,String> map = new HashMap<String,String>();
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xml);
			Element rootElt = doc.getRootElement();
			getNodes(rootElt, map);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return map;
	}

}
